package com.px.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.px.dal.AwardsMapper;
import com.px.dal.ProjectInfoMapper;
import com.px.dal.ProjectSpecialistRelMapper;
import com.px.dal.SpecialInfoMapper;
import com.px.model.AwardInfo;
import com.px.model.PageParams;
import com.px.model.ProjectInfo;
import com.px.model.ProjectSpecialistRel;
import com.px.model.SpeciallistInfo;
import com.px.model.getData;
import com.px.unit.Response;

//把当前类注解为控制器
@Controller
public class SpecialInfoController {

	// 实例化我们的数据操作类
	SpecialInfoMapper special_mapper = new SpecialInfoMapper();
	ProjectInfoMapper project_mapper = new ProjectInfoMapper();
	ProjectSpecialistRelMapper project_specialist_rel_mapper = new ProjectSpecialistRelMapper();
	AwardsMapper award_mapper = new AwardsMapper();

	// 专家登陆
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/validateSpecialLogin", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response validateSpecialLogin(@RequestBody SpeciallistInfo speciallistInfo) {
		SpeciallistInfo speciallist = special_mapper.validateSpecialLogin(speciallistInfo);
		getData mydata = new getData();
		mydata.setCurrentSpeciallistInfo(speciallist);
		if (speciallist != null) {
			// 登录成功
			return new Response(200, "登录成功", mydata);
		} else {
			// 登录失败
			return new Response(403, "登录失败");
		}
	}

	/**
	 * 
	 * 添加专家信息提交方法
	 * 前端交互为vuecli框架
	 * @parma 参数为专家model对象
	 * */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/addSpecialInfo", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response addSpecialInfo(@RequestBody SpeciallistInfo user) {

		if (user != null) {

			// 判断添加的专家登录账户不能重复
			int checkname = special_mapper.checkSpecialNameIsExist(user);
			if (checkname > 0) {
				return new Response(304, "已存在同名帐号");
			} else {
				int counts = special_mapper.insertDemoUser(user);
				if (counts > 0) {
					return new Response(200, "添加成功！");
				} else {
					return new Response(304, "添加失败！");
				}
			}
		}
		return new Response(304, "添加失败！");
	}

	/**
	 * 查询所有专家信息列表方法
	 * 
	 * */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getSpecialUserList", method = RequestMethod.GET, produces = "application/json; utf-8")
	public Response getSpecialUserList() {

		// 示例化一个自定义对象，用来传递我们需要的参数
		getData mydata = new getData();
		List<SpeciallistInfo> speciallist = special_mapper.getSpecialInfoList();
		mydata.setSpeciallist(speciallist);
		return new Response(200, "获取成功！", mydata);
	}

	/**
	 * 初始化查询专家信息列表分页方法
	 * 
	 * */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getSpecialUserListLimit", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response getSpecialUserListLimit(@RequestBody PageParams page) {

		// 示例化一个自定义对象，用来传递我们需要的参数
		getData mydata = new getData();
		mydata.setCounts(special_mapper.getAllSpecialCounts());
		int index = (page.getCurrentPage() - 1) * page.getPageSize();
		int pagesize = page.getPageSize();

		System.out.println("index:" + index + " pagesize:" + pagesize);

		List<SpeciallistInfo> speciallist = special_mapper.getSpecialInfoListLimit(index, pagesize);

		mydata.setSpeciallist(speciallist);
		return new Response(200, "获取成功！", mydata);
	}

	/**
	 * 查询专家信息列表分页方法
	 * 
	 * */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getSpecialInfoBySearchLimit", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response getSpecialInfoBySearchLimit(@RequestBody PageParams page) {
		System.out.println("search:" + page.getSearch());
		// 设置默认分页
		int index = (page.getCurrentPage() - 1) * page.getPageSize();
		int pagesize = page.getPageSize();
		// 示例化一个自定义对象，用来传递我们需要的参数
		getData mydata = new getData();
		// 取搜索结果的数据条数
		mydata.setCounts(special_mapper.getSpecialCountsBySearch(page.getSearch()));
		// 取搜索结果的数据内容List
		mydata.setSpeciallist(special_mapper.getSpecialInfoBySearchLimit(page.getSearch(), index, pagesize));
		return new Response(200, "获取成功！", mydata);
	}

	/**
	 * 根据专家用户名删除专家信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/deleteSpecialByUserName", method = RequestMethod.DELETE, produces = "application/json; utf-8")
	public Response deleteSpecialByUserName(@RequestBody SpeciallistInfo speciallistInfo) {

		if (speciallistInfo != null) {

			// 判断删除的专家是否存在
			int checkname = special_mapper.checkSpecialNameIsExist(speciallistInfo);
			if (!(checkname > 0)) {
				return new Response(304, "该专家不存在");
			} else {
				int counts = special_mapper.deleteSpecialBySpecialName(speciallistInfo);
				if (counts > 0) {
					return new Response(200, "删除成功！");
				} else {
					return new Response(304, "删除失败！");
				}
			}
		}
		return new Response(304, "删除失败！");
	}

	/**
	 * 根据专家ID删除专家信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/deleteSpecialByUserId", method = RequestMethod.DELETE, produces = "application/json; utf-8")
	public Response deleteSpecialByUserId(@RequestBody SpeciallistInfo speciallistInfo) {

		if (speciallistInfo != null) {

			// 判断删除的专家是否存在
			int checkid = special_mapper.checkSpecialIdIsExist(speciallistInfo);
			if (!(checkid > 0)) {
				return new Response(304, "该专家不存在");
			} else {
				int counts = special_mapper.deleteSpecialBySpecialId(speciallistInfo);
				if (counts > 0) {
					return new Response(200, "删除成功！");
				} else {
					return new Response(304, "删除失败！");
				}
			}
		}
		return new Response(304, "删除失败！");
	}

	/**
	 * 根据id修改专家信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/updateSpecialBySpecialId", method = RequestMethod.PUT, produces = "application/json; utf-8")
	public Response updateSpecialBySpecialId(@RequestBody SpeciallistInfo speciallistInfo) {

		if (speciallistInfo != null) {

			// 判断修改的专家是否存在
			int checkid = special_mapper.checkSpecialIdIsExist(speciallistInfo);
			if (!(checkid > 0)) {
				return new Response(304, "该专家不存在");
			} else {
				int counts = special_mapper.updateSpecialBySpecialId(speciallistInfo);
				if (counts > 0) {
					return new Response(200, "修改成功！");
				} else {
					return new Response(304, "修改失败！");
				}
			}
		}
		return new Response(304, "修改失败！");
	}

	/**
	 * 根据用户名修改专家信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/updateSpecialBySpecialName", method = RequestMethod.PUT, produces = "application/json; utf-8")
	public Response updateSpecialBySpecialName(@RequestBody SpeciallistInfo speciallistInfo) {

		if (speciallistInfo != null) {

			// 判断修改的专家是否存在
			int checkname = special_mapper.checkSpecialNameIsExist(speciallistInfo);
			if (!(checkname > 0)) {
				return new Response(304, "该专家不存在");
			} else {
				int counts = special_mapper.updateSpecialBySpecialName(speciallistInfo);
				if (counts > 0) {
					return new Response(200, "修改成功！");
				} else {
					return new Response(304, "修改失败！");
				}
			}
		}
		return new Response(304, "修改失败！");
	}

	/**
	 * 获取指定Id专家信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getSpecialInfoBySpecialId", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response getSpecialInfoBySpecialId(@RequestBody SpeciallistInfo speciallistInfo) {
		if (speciallistInfo != null) {
			// 判断查询的专家是否存在
			int checkid = special_mapper.checkSpecialIdIsExist(speciallistInfo);
			if (!(checkid > 0)) {
				return new Response(304, "该专家不存在");
			} else {
				// 示例化一个自定义对象，用来传递我们需要的参数
				getData mydata = new getData();
				List<SpeciallistInfo> speciallist = special_mapper.getSpecialInfoBySpecialId(speciallistInfo);
				mydata.setSpeciallist(speciallist);
				return new Response(200, "获取成功！", mydata);
			}
		}
		return new Response(304, "获取失败！");

	}

	// 对项目进行评分
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/scoringTheProject", method = RequestMethod.PUT, produces = "application/json; utf-8")
	public Response scoringTheProject(@RequestBody ProjectSpecialistRel projectSpecialistRel) {
		if (projectSpecialistRel != null) {
			// 判断指定专家是否存在
			SpeciallistInfo speciallistInfo = new SpeciallistInfo();
			speciallistInfo.setSpecialist_id(projectSpecialistRel.getSpecialist_id());
			int checkspecialid = special_mapper.checkSpecialIdIsExist(speciallistInfo);
			if (!(checkspecialid > 0)) {
				return new Response(304, "该专家不存在");
			} else {
				// 判断指定项目是否存在
				ProjectInfo projectInfo1 = new ProjectInfo();
				projectInfo1.setProject_info_id(projectSpecialistRel.getProject_info_id());
				int checkprojectid = project_mapper.checkProjectInfoIdIsExist(projectInfo1);
				if (!(checkprojectid > 0)) {
					return new Response(304, "该项目不存在");
				} else {
					// TODO 还需要判断该专家是否已对该项目评过分
					int checkisscore = project_specialist_rel_mapper.checkScoreIsExist(projectSpecialistRel);
					if (checkisscore > 0) {
						return new Response(304, "该专家已评分");
					} else {
						int issuccess1 = project_specialist_rel_mapper.insertProjectSpecialistRel(projectSpecialistRel);
						if (issuccess1 > 0) {
							// 关系表添加成功
							ProjectInfo projectInfo2 = project_mapper.getProjectByProjectInfoId(projectInfo1).get(0);
							int sum = projectInfo2.getCounts();
							sum = sum + projectSpecialistRel.getSpecialist_score();
							projectInfo2.setCounts(sum);
							// 获取给该项目评分的专家数量
							int sum_speciallist = project_specialist_rel_mapper
									.getAllSpecialistCountsByProjectInfoId(projectSpecialistRel);
							// 计算平均分
							int average = sum / sum_speciallist;
							projectInfo2.setAverage(average);
							int issuccess2 = project_mapper.updateProjectScoreByProjectInfoId(projectInfo2);
							if (issuccess2 > 0) {
								// 评分成功
								return new Response(200, "评分成功！");
							} else {
								return new Response(304, "评分失败！");
							}
						} else {
							// 关系表添加失败
							return new Response(304, "评分失败！");
						}
					}
				}
			}
		}
		return new Response(304, "评分失败！");

	}

	// 获取得奖项目信息
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getAwardWinningProjects", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response getAwardWinningProjects(@RequestBody AwardInfo awardInfo) {
		if (awardInfo != null) {
			// 判断查询的奖项是否存在
			int checkid = award_mapper.checkAdwardsIdIsExist(awardInfo);
			if (!(checkid > 0)) {
				return new Response(304, "该奖项不存在");
			} else {
				// 获取指定id的奖项信息
				AwardInfo awardInfo1 = award_mapper.selectAdwardsByAdardsId(awardInfo).get(0);
				// 获取奖项总数
				int sum_awards = awardInfo1.getPrize_1() + awardInfo1.getPrize_2() + awardInfo1.getPrize_3();
				// 获取评审组信息
				int review_group = awardInfo1.getReview_id();
				// 查询获奖列表
				List<ProjectInfo> projectInfos = project_mapper.getFirstProjects(review_group, sum_awards);
				// 示例化一个自定义对象，用来传递我们需要的参数
				getData mydata = new getData();
				mydata.setProjectWinInfolist(projectInfos);
				return new Response(200, "获取成功！", mydata);
			}
		}
		return new Response(304, "获取失败！");

	}
}
